import * as comm from '../../../utils/common';

// 获取 App 实例
const appInstance = getApp();
Component({
  properties: {
    device: {
      type: Object,
      value: {
        deviceId: null,
        totalSwitch: false,
        name: '设备名称',
        status: '当前行程',
        power: false,
        startTime: '--:--',
        endTime: '--:--',
        temperature: '30',
        //bluetoothStatus: '00', // 蓝牙状态：00-未连接，01-已连接，04-连接中，05-连接错误
        //wifiStatus: '02', // WiFi状态：02-未连接，03-已连接，06-连接中，07-连接错误
        fanStatus: '06', // 风扇状态：06-未启动，07-已启动
        online: 0,
        topicSend: '',
        topicSub: '',
      },
    },
    index: {
      type: Number,
      value: 0,
    },
    disableNavigation: {
      type: Boolean,
      value: false
    },
  },

  data: {
    longPressThreshold: 3000, // 长按阈值3秒
    channels: [],
  },

  lifetimes: {
    attached() {
      console.log("attached...")
    },

    detached() {
      console.log("detached...")
    }
  },

  methods: {
    initVal(){

    },

    /*
    // 切换设备电源状态
    togglePower() {
      const power = !this.data.device.power
      const index = this.data.index

      // 导入storage服务
      const storageService = require('../../../services/storage')

      // 更新本地存储中的设备状态
      const devices = storageService.getDevices() || []
      devices[index].power = power
      storageService.saveDevices(devices)

      this.setData({
        'device.power': power,
      })
    },
    */

    // 长按设备卡片处理（删除设备）
    handleLongPress(e) {
      if (e.target.dataset.type === 'name' || e.target.dataset.type === 'power') {
        return
      }
      wx.showModal({
        title: '删除设备',
        content: '确定要删除此设备吗？',
        success: res => {
          if (res.confirm) {
            this.triggerEvent('deleteDevice', {
              index: this.data.index
            })
          }
        },
      })
    },

    // 长按设备名称处理（修改名称）
    handleNameLongPress(e) {
      if (e && e.stopPropagation) {
        e.stopPropagation()
      }
      wx.showModal({
        title: '修改设备名称',
        content: '',
        editable: true,
        placeholderText: '请输入新的设备名称',
        success: res => {
          if (res.confirm && res.content) {
            const index = this.data.index
            // 触发nameChange事件，通知父组件名称已更改
            this.triggerEvent('nameChange', {
              index: index,
              name: res.content,
            })
          }
        },
      })
    },

    // 点击设备卡片跳转到模式设置页面R
    navigateToModeSetting(e) {
      if (e.target.dataset.type === 'total' || e.target.dataset.type === 'name' || e.target.dataset.type === 'device-info') {
        return
      }
      let did = this.data.device.deviceId
      let url = '/packageB/pages/device/index?did=' + did;
      let tvobj = appInstance.deviceVerMap[did];
      if (!comm.isEmptyObj(tvobj)) {
        if (tvobj.t == "C" && tvobj.v > 11) {
          url = '/packageC/pages/device/index?did=' + did;
        }
      }
      wx.redirectTo({
        url: url
      });

    },

  },



})